package com.wanxi.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanxi.dao.NavDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.Contact;
import com.wanxi.model.Nav;
import com.wanxi.service.NavService;
import com.wanxi.util.CreateOnlyID;
import com.wanxi.util.JsonCopy;
import com.wanxi.vo.NavVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Chengbo Zhang
 */
@Service
@Transactional
public class NavServiceImpl implements NavService {
    @Autowired
    NavDao navDao;
    @Autowired
    CreateOnlyID createOnlyId;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public ResultDTO findAllNav() {
        QueryWrapper<Nav> queryWrapper=new QueryWrapper<Nav>()
                .orderByDesc("id");
        List<Nav> list = navDao.selectList(queryWrapper);
        if (list.size() > 0) {
            return ResultDTO.getDTO(list.size(), list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }

    @Override
    public ResultDTO findNavById(NavVO navVO) {
        Nav nav = JsonCopy.copy(navVO, Nav.class);
        Nav n = navDao.selectById(nav.getId());
        if (n != null) {
            return ResultDTO.getDTO(1, n);
        }
        return ResultDTO.builder().count(0).msg("error").code(1).build();
    }

    @Override
    public ResultDTO updateEnable(NavVO navVO) {
        Nav nav = JsonCopy.copy(navVO, Nav.class);
        nav.setUpdateTime(new Date());
        int i = navDao.updateById(nav);
        if (i == 1) {
            stringRedisTemplate.delete("nav");
            return ResultDTO.getDTO(i, navVO);
        }
        return ResultDTO.builder().data(navVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO addNav(NavVO navVO) {
        Nav nav = JsonCopy.copy(navVO, Nav.class);
        nav.setId(createOnlyId.createId());
        nav.setCreateTime(new Date());
        nav.setUpdateTime(new Date());
        int i = navDao.insert(nav);
        if (i == 1) {
            stringRedisTemplate.delete("nav");
            return ResultDTO.getDTO(i, navVO);
        }
        return ResultDTO.builder().data(navVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO update(NavVO navVO) {
        Nav nav = JsonCopy.copy(navVO, Nav.class);
        nav.setUpdateTime(new Date());
        int i = navDao.updateById(nav);
        if (i == 1) {
            stringRedisTemplate.delete("nav");
            return ResultDTO.getDTO(i, navVO);
        }
        return ResultDTO.builder().data(navVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO delete(NavVO navVO) {
        Nav nav = JsonCopy.copy(navVO, Nav.class);
        int i = navDao.deleteById(nav);
        if (i == 1) {
            stringRedisTemplate.delete("nav");
            return ResultDTO.getDTO(i, navVO);
        }
        return ResultDTO.builder().data(navVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO findNav() {
        Long len = stringRedisTemplate.opsForList().size("nav");

//        判断redis中是否有数据
        if (len != 0) {
            List<String> redisNav =stringRedisTemplate.opsForList().range("nav",0,len);
//            返回redis中的数据,通过fastJson转换
            List<Nav> list = JSON.parseObject(redisNav.toString(), new TypeReference<List<Nav>>() {
            });
            return ResultDTO.getDTO(list.size(), list);
        }
//        没有则从数据库中找数据
        List<Nav> list = navDao.selectList(null);
        if (list.size() > 0) {
//            Dao层数据迭代
            Iterator<Nav> iterator = list.iterator();
            while (iterator.hasNext()) {
                Nav n = iterator.next();
                if (n.getEnable() == 0) {
                    iterator.remove();
                } else {
//                    将查到的数据放入redis
                    stringRedisTemplate.expire("nav",300, TimeUnit.SECONDS);
                    stringRedisTemplate.opsForList().rightPush("nav", JSON.toJSONString(n));
                }
            }
            return ResultDTO.getDTO(list.size(), list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }
}
